// 1. 从本地存储获取用户名设置到首页右上角
function setUserName() {
  // 获取本地存储数据
  let uname = localStorage.getItem('username')
  const userBox = document.querySelector('.username')
  if (uname) {
    userBox.innerHTML = uname
  } else {
    userBox.innerHTML = ''
  }
}
setUserName()

// 2. 获取学生统计数据并且携带header参数完成jwt验证流程
async function getStudentData() {
  try {
    let token = localStorage.getItem('token')
    let res = await axios({
      url: '/dashboard',
      headers: {
        Authorization: token
      }
    })
    let { overview, year, salaryData, groupData, provinceData } = res.data.data
    for (key in overview) {
      document.querySelector(`.${key}`).innerHTML = overview[key]
    }
    // 渲染平均薪资现状图
    renderLine(year)
    // 班级薪资分布
    renderCircle(salaryData)
    // 每组薪资
    renderLines(groupData)
    // 男女薪资分布
    renderGender(salaryData)
    // 籍贯分布
    renderMap(provinceData)
  } catch (err) {
    // console.dir(err)
    // 如果err.response.status等于401表示服务器验证token失败
    // 此时应该跳转到登录页面，让用户重新登录 获取新token
    if (err.response.status == 401) {
      pxmu.toast('登录过期，请重新登录')
      setTimeout(() => {
        location.href = 'login.html'
      }, 1000)
    }
  }
}
getStudentData()

// 3. 退出登录
document.querySelector('#logout').addEventListener('click', async function () {
  let dialog = await pxmu.diaglog('您确定退出吗?')
  if (dialog.btn == 'right') {
    // 清除本地缓存
    localStorage.removeItem('token')
    localStorage.removeItem('username')
    // 跳转到页面
    location.href = 'login.html'
  }
})

// 薪资走势
function renderLine(year) {
  let echars = echarts.init(document.querySelector('#line'))
  const option = {
    xAxis: {
      type: 'category',
      boundaryGap: false,
      data: year.map(item => item.month) //['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        data: year.map(item => item.salary),//[820, 932, 901, 934, 1290, 1330, 1320],
        type: 'line',
        smooth: true,
        lineStyle: {
          width: 5,
          // 颜色
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 1,
            y2: 0,
            colorStops: [{
              offset: 0, color: '#479dee' // 0% 处的颜色
            }, {
              offset: 1, color: '#5c75f0' // 100% 处的颜色
            }],
            global: false // 缺省为 false
          }
        },
        areaStyle: {
          color: {
            type: 'linear',
            x: 0,
            y: 0,
            x2: 0,
            y2: 1,
            colorStops: [{
              offset: 0, color: '#b2d7f7' // 0% 处的颜色
            }, {
              offset: 1, color: 'rgba(255,255,255,0)' // 100% 处的颜色
            }],
            global: false // 缺省为 false
          }
        }
      }
    ]
  }
  echars.setOption(option)
}

// 薪资分布
function renderCircle(lesson) {
  let echars = echarts.init(document.querySelector('#salary'))
  const option = {
    title: {
      text: '班级薪资分布',
      left: 10,
      top: 15
    },
    color: ['#fda224', '#5097ff', '#3abcfa', '#34d39a'],
    tooltip: {
      trigger: 'item'
    },
    legend: {
      bottom: '5%',
      left: 'center'
    },
    series: [
      {
        name: '班级薪资分布',
        type: 'pie',
        radius: ['48%', '60%'],
        avoidLabelOverlap: false,
        itemStyle: {
          borderRadius: 15,
          borderColor: '#fff',
          borderWidth: 2
        },
        // 饼状图说明文本
        label: {
          show: false,
          // show: true,
          // position: 'center'
        },
        // 说明文本的指引线
        labelLine: {
          show: false
          // show: true
        },
        // 是否启用防止标签重叠策略，默认开启
        // avoidLabelOverlap: false,
        data: lesson.map(item => {
          return {
            value: item.g_count + item.b_count,
            name: item.label
          }
        })
        //   [
        //   { value: 1048, name: 'Search Engine' },
        //   { value: 735, name: 'Direct' },
        //   { value: 580, name: 'Email' },
        //   { value: 484, name: 'Union Ads' },
        //   { value: 300, name: 'Video Ads' }
        // ]
      }
    ]
  }
  echars.setOption(option)
}

// 每组薪资
function renderLines(group) {
  let echars = echarts.init(document.querySelector('#lines'))
  const option = {
    tooltip: {
      trigger: 'item'
    },
    calculable: true,
    xAxis: [
      {
        type: 'category',
        data: group[1].map(item => item.name)
        // ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
      }
    ],
    yAxis: [
      {
        type: 'value',
        // 分割线的类型
        splitLine: {
          lineStyle: {
            type: 'dashed'
          }
        }
      }
    ],
    series: [
      {
        name: '期望薪资',
        type: 'bar',
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#44D4A1' },
            { offset: 1, color: '#CAF3E5' }
          ])
        },
        data: group[1].map(item => item.hope_salary)
        // [
        //   22, 44, 77, 23, 25.6, 76.7, 85, 80, 32.6, 20.0, 6.4, 3.3
        // ]
      },
      {
        name: '实际薪资',
        type: 'bar',
        itemStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: '#519AE5' },
            { offset: 1, color: '#D2E9F9' }
          ])
        },
        data: group[1].map(item => item.salary)
        // [
        //   2.6, 5.9, 9.0, 26.4, 28.7, 70.7, 76, 90, 48.7, 18.8, 6.0, 2.3
        // ]
      }
    ]
  }
  echars.setOption(option)

  // 高亮切换
  document.querySelector('#btns').addEventListener('click', (e) => {
    if (e.target.classList.contains('btn')) {
      document.querySelector('.btn-blue').classList.remove('btn-blue')
      e.target.classList.add('btn-blue')
      // 数据切换
      const data = group[e.target.innerText]
      option.xAxis[0].data = data.map(item => item.name)
      option.series[0].data = data.map(item => item.hope_salary)
      option.series[1].data = data.map(item => item.salary)
      // 重新渲染
      echars.setOption(option)
    }
  })
}

// 男女薪资分布
function renderGender(gender) {
  let echars = echarts.init(document.querySelector('#gender'))
  const option = {
    title: [{
      text: '男女薪资分布',
      top: '10',
      left: '10'
    },
    {
      text: '男生',
      left: '50%',
      top: '45%',
      textStyle: {
        fontSize: 12
      },
      textAlign: 'center'
    },
    {
      text: '女生',
      left: '50%',
      top: '85%',
      textStyle: {
        fontSize: 12
      },
      textAlign: 'center'
    }],
    color: ['#fda224', '#5097ff', '#3abcfa', '#34d39a'],
    tooltip: {
      trigger: 'item'
    },
    series: [
      {
        type: 'pie',
        radius: ['20%', '30%'],
        // 饼图的中心（圆心）坐标，数组的第一项是横坐标，第二项是纵坐标
        center: ['50%', '30%'],
        data: gender.map(item => {
          return { value: item.b_count, name: item.label }
        })
        // [
        //   { value: 1048, name: 'Search Engine' },
        //   { value: 735, name: 'Direct' },
        //   { value: 580, name: 'Email' },
        //   { value: 484, name: 'Union Ads' },
        //   { value: 300, name: 'Video Ads' }
        // ]
      },
      {
        type: 'pie',
        radius: ['20%', '30%'],
        center: ['50%', '70%'],
        data: gender.map(item => {
          return { value: item.g_count, name: item.label }
        })
        // [
        //   { value: 1048, name: 'Search Engine' },
        //   { value: 735, name: 'Direct' },
        //   { value: 580, name: 'Email' },
        //   { value: 484, name: 'Union Ads' },
        //   { value: 300, name: 'Video Ads' }
        // ]
      }]
  }
  echars.setOption(option)
}

// 籍贯分布
function renderMap(province) {
  let echars = echarts.init(document.querySelector('#map'))
  const dataList = [
    { name: '南海诸岛', value: 0 },
    { name: '北京', value: 0 },
    { name: '天津', value: 0 },
    { name: '上海', value: 0 },
    { name: '重庆', value: 0 },
    { name: '河北', value: 0 },
    { name: '河南', value: 0 },
    { name: '云南', value: 0 },
    { name: '辽宁', value: 0 },
    { name: '黑龙江', value: 0 },
    { name: '湖南', value: 0 },
    { name: '安徽', value: 0 },
    { name: '山东', value: 0 },
    { name: '新疆', value: 0 },
    { name: '江苏', value: 0 },
    { name: '浙江', value: 0 },
    { name: '江西', value: 0 },
    { name: '湖北', value: 0 },
    { name: '广西', value: 0 },
    { name: '甘肃', value: 0 },
    { name: '山西', value: 0 },
    { name: '内蒙古', value: 0 },
    { name: '陕西', value: 0 },
    { name: '吉林', value: 0 },
    { name: '福建', value: 0 },
    { name: '贵州', value: 0 },
    { name: '广东', value: 0 },
    { name: '青海', value: 0 },
    { name: '西藏', value: 0 },
    { name: '四川', value: 0 },
    { name: '宁夏', value: 0 },
    { name: '海南', value: 0 },
    { name: '台湾', value: 0 },
    { name: '香港', value: 0 },
    { name: '澳门', value: 0 },
  ]
  // 筛选数据
  dataList.forEach(item => {
    const res = province.find(v => {
      return v.name.includes(item.name)
    })
    // console.log(res)
    // 数据赋值
    if (res !== undefined) {
      item.value = res.value
    }
  })
  // console.log(dataList)
  const option = {
    title: {
      text: '籍贯分布',
      top: 10,
      left: 10,
      textStyle: {
        fontSize: 16
      }
    },
    tooltip: {
      trigger: 'item',
      formatter: '{b}: {c} 位学员',
      borderColor: 'transparent',
      backgroundColor: 'rgba(0,0,0,0.5)',
      textStyle: {
        color: '#fff'
      }
    },
    visualMap: {
      min: 0,
      max: 6,
      left: 'left',
      bottom: '20',
      text: ['6', '0'],
      inRange: {
        color: ['#ffffff', '#0075F0']
      },
      show: true,
      left: 40
    },
    geo: {
      map: 'china',
      roam: false,
      zoom: 1.0,
      label: {
        normal: {
          show: true,
          fontSize: '10',
          color: 'rgba(0,0,0,0.7)'
        }
      },
      itemStyle: {
        normal: {
          borderColor: 'rgba(0, 0, 0, 0.2)',
          color: '#e0ffff'
        },
        emphasis: {
          areaColor: '#34D39A',
          shadowOffsetX: 0,
          shadowOffsetY: 0,
          shadowBlur: 20,
          borderWidth: 0,
          shadowColor: 'rgba(0, 0, 0, 0.5)'
        }
      }
    },
    series: [
      {
        name: '籍贯分布',
        type: 'map',
        geoIndex: 0,
        data: dataList
      }
    ]
  }
  echars.setOption(option)
}

